from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods


class SKUSerialzier(serializers.ModelSerializer):
    class Meta:
        # 指定生成序列化字段的模型类
        model = SKU
        # 指定字段
        fields = ('name', 'default_image')



class OrderGoodsSerializer(serializers.ModelSerializer):
    """
    订单商品表序列化器
    """
    sku = SKUSerialzier(read_only=True)
    class Meta:
        # 指定生成序列化字段的模型类
        model = OrderGoods
        # 指定字段
        fields = ('count', 'price','sku')

class OrderInfoSerializer(serializers.ModelSerializer):
    """
    订单信息表序列化器
    """
    # user表是父表  order为子表   子表嵌套父表返回
    user = serializers.StringRelatedField(read_only=True)
    # 订单信息表是父表  订单商品表是子表 父表嵌套子表返回
    skus = OrderGoodsSerializer(read_only=True,many=True)
    class Meta:
        # 指定生成序列化字段的模型类
        model = OrderInfo
        # 指定字段
        fields = "__all__"